package logic;

import ui.ScreenWB;
import db.BankDatabase;
//import db.atmDatabase;

// Withdrawal.java
// Represents a withdrawal ATM transaction

public class Withdrawal extends Transaction {
	private int amount; // amount to withdraw
	private CashDispenser cashDispenser; // reference to cash dispenser

	// Withdrawal constructor
	public Withdrawal(int userAccountNumber, ScreenWB atmScreen,
			BankDatabase atmBankDatabase, CashDispenser atmCashDispenser) {
		// initialize superclass variables
		super(userAccountNumber, atmScreen, atmBankDatabase);

		// initialize references to keypad and cash dispenser
		cashDispenser = atmCashDispenser;
		
	} // end Withdrawal constructor


	public void execute() {
		boolean cashDispensed = false; // cash was not dispensed yet
		double availableBalance; // amount available for withdrawal

		amount = screenWB.getWithdraw().getWithdrawAmount(); // get withdraw
															// amount from user

		// get available balance of account involved
		availableBalance = bankDatabase.getBalance(accountNumber);

		// check whether the user has enough money in the account
		if (amount <= availableBalance) {
			// check whether the cash dispenser has enough money
			if (cashDispenser.isSufficientCashAvailable(amount)) {
				// update the account involved to reflect the withdrawal
				bankDatabase.debit(accountNumber, amount);

				cashDispenser.dispenseCash(amount); // dispense cash
				cashDispensed = true; // cash was dispensed

				// instruct user to take cash
				screenWB.displayMessageLine("\nYour cash has been"
						+ " dispensed. Please take your cash now.");
			} // end if
			else
				// cash dispenser does not have enough cash
				screenWB.displayMessageLine("\nInsufficient cash available in the ATM."
						+ "\n\nPlease choose a smaller amount.");
		} // end if
		else // not enough money available in user's account
		{
			screenWB.displayMessageLine("\nInsufficient funds in your account."
					+ "\nPlease choose a smaller amount.");
		} // end else
	} // end if

} // end class Withdrawal

